package com.trkj.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.jpa.CcBerthDAO;
import com.trkj.dao.jpa.CcGenreDAO;
import com.trkj.dao.jpa.CcHospFloorDAO;
import com.trkj.dao.jpa.CcPatientDAO;
import com.trkj.dao.jpa.CcStoreyDAO;
import com.trkj.dao.jpa.CcWardDAO;
import com.trkj.dao.jpa.IYzrTbstaffDAO;
import com.trkj.dao.jpa.TbdepartmentDAO;
import com.trkj.pojos.CcBerth;
import com.trkj.pojos.CcGenre;
import com.trkj.pojos.CcPatient;
import com.trkj.pojos.CcStorey;
import com.trkj.pojos.CcWard;
import com.trkj.pojos.Tbdepartment;
import com.trkj.pojos.Tbstaff;
import com.trkj.vo.CcBerthVo;
import com.trkj.vo.CcStoreyVo;
import com.trkj.vo.CcWardVo;

@Service
@Transactional
public class CcBerthService {

	@Autowired
	private CcBerthDAO ccBerthDAO;

	@Autowired
	private CcWardDAO ccWardDAO;
	@Autowired
	private IYzrTbstaffDAO staffDAO;
	@Autowired
	private TbdepartmentDAO tbdeDAO;
	@Autowired
	private CcPatientDAO ccpDAO;
	@Autowired
	private CcHospFloorDAO ccHospDAO;
	@Autowired
	private CcGenreDAO ccGDAO;
	@Autowired
	private CcStoreyDAO ccSDAO;

	public void updateBerth(long bid, BigDecimal pid) {
		CcBerth cc = ccBerthDAO.findOne(bid);
		cc.setBpId(pid);
		cc.setBerthstate("已住人");
		upWberth(cc.getBwardno().longValue());
		ccBerthDAO.save(cc);
	}

	public void upWberth(long id) {
		CcWard cc = ccWardDAO.findOne(id);
		cc.setWpeople((Integer.parseInt(cc.getWpeople()) + 1) + "");
		ccWardDAO.save(cc);
	}

	public List<CcWard> queryWname(String wname) {
		return ccWardDAO.findByWname(wname);
	}

	public List<Tbdepartment> querydepartmentName(String departmentName) {
		return tbdeDAO.queryByName(departmentName);
	}

	public List<CcGenre> findByGename(String gename) {
		return ccGDAO.findByGename(gename);
	}

	// 根据病房类别查询
	public List<CcBerthVo> queryByGename(String gename) {
		List<CcBerth> list = (List<CcBerth>) ccBerthDAO.findAll();
		List<CcGenre> ccg = findByGename(gename);
		List<CcBerthVo> ccB = new ArrayList<>();
		List<CcBerth> cB = new ArrayList<>();
		for (CcGenre cg : ccg) {
			List<CcWard> lw = ccWardDAO.findByWgenreid(new BigDecimal( cg.getGenreid()));
			for (CcWard cc : lw) {
				for (CcBerth cb : list) {
					if (cc.getWardid() == cb.getBwardno().longValue()) {
						CcBerth e = new CcBerth();
						e.setBerthno(cb.getBerthno());
						e.setBerthstate(cb.getBerthstate());
						e.setBname(cb.getBname());
						e.setBpId(cb.getBpId());
						e.setBwardno(cb.getBwardno());
						cB.add(e);
					}
				}
			}
		}
		ccB = poberthList2VoList(cB);
		return ccB;
	}
	
	// 根据科室名称查询
	public List<CcBerthVo> queryBydepartmentName(String departmentName) {
		List<CcBerth> list = (List<CcBerth>) ccBerthDAO.findAll();
		List<Tbdepartment> tb = querydepartmentName(departmentName);
		List<CcBerthVo> ccB = new ArrayList<>();
		List<CcBerth> cB = new ArrayList<>();
		for (Tbdepartment tbd : tb) {
			List<CcWard> lw = ccWardDAO.findByWofficesId(new BigDecimal(tbd.getDepartmentNo()));
			for (CcWard cc : lw) {
				for (CcBerth cb : list) {
					if (cc.getWardid() == cb.getBwardno().longValue()) {
						CcBerth e = new CcBerth();
						e.setBerthno(cb.getBerthno());
						e.setBerthstate(cb.getBerthstate());
						e.setBname(cb.getBname());
						e.setBpId(cb.getBpId());
						e.setBwardno(cb.getBwardno());
						cB.add(e);
					}
				}
			}
		}
		ccB = poberthList2VoList(cB);
		return ccB;
	}

	// 病房名称查询
	public List<CcBerthVo> queryByWname(String wname) {
		List<CcBerth> list = (List<CcBerth>) ccBerthDAO.findAll();
		List<CcWard> lw = queryWname(wname);
		List<CcBerthVo> ccB = new ArrayList<>();
		List<CcBerth> cB = new ArrayList<>();
		for (CcWard cc : lw) {
			for (CcBerth cb : list) {
				if (cc.getWardid() == cb.getBwardno().longValue()) {
					CcBerth e = new CcBerth();
					e.setBerthno(cb.getBerthno());
					e.setBerthstate(cb.getBerthstate());
					e.setBname(cb.getBname());
					e.setBpId(cb.getBpId());
					e.setBwardno(cb.getBwardno());
					cB.add(e);
				}
			}
		}
		ccB = poberthList2VoList(cB);
		return ccB;
	}

	public Map<String, Object> findByberthstate(String state,int page,int rows) {
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcBerth> list = ccBerthDAO.findByberthstate(pr,state);
		Map<String, Object> map = new HashMap<>();
		map.put("total", ccBerthDAO.findByCount(state));
		map.put("rows", poberthList2VoList(list));
		return map;
	}

	public Map<String, Object> queryALLberth(int page, int rows) {
		PageRequest pr = new PageRequest(page - 1, rows);
		List<CcBerth> list = ccBerthDAO.findByAll(pr);
		Map<String, Object> map = new HashMap<>();
		map.put("total", ccBerthDAO.count());
		map.put("rows", poberthList2VoList(list));
		return map;
	}
	public Map<String, Object> querywberth(String bwardno) {
		List<CcBerth> list = ccBerthDAO.findBywb(new BigDecimal(bwardno));
		Map<String, Object> map = new HashMap<>();
		
		map.put("total", list.size());
		map.put("rows", poberthList2VoList(list));
		return map;
	}
	private List<CcBerthVo> poberthList2VoList(List<CcBerth> list) {
		if (list == null)
			return null;
		List<CcBerthVo> listVo = new ArrayList<>();

		for (CcBerth cc : list) {
			CcBerthVo ccVo = new CcBerthVo();
			ccVo.setBerthno(cc.getBerthno());
			ccVo.setBname(cc.getBname());
			ccVo.setBerthstate(cc.getBerthstate());
			ccVo.setWardid(cc.getBwardno().longValue());
			CcWard ccWard = ccWardDAO.findOne(ccVo.getWardid());
			ccVo.setWname(ccWard.getWname());
			if (cc.getBpId() != null) {
				ccVo.setPatientid(cc.getBpId().longValue());
				CcPatient cp = ccpDAO.findOne(ccVo.getPatientid());
				if(cp!=null){
					ccVo.setPsex(cp.getPsex());
					ccVo.setPname(cp.getPname());
					ccVo.setIcid(cp.getIcid());
					if (cp.getPadoctorid() != null) {
						ccVo.setPadoctorid(cp.getPadoctorid());
						Tbstaff staff = staffDAO.findOne(ccVo.getPadoctorid().longValue());
						ccVo.setPadoctorname(staff.getStaffName());
					}
				}
				
				
			}
			ccVo.setPofficesid(ccWard.getWofficesId().longValue());
			ccVo.setPofficesname(tbdeDAO.queryName(ccVo.getPofficesid()));

			ccVo.setGenreid(ccWard.getWgenreid().longValue());
			ccVo.setGename(ccGDAO.queryName(ccVo.getGenreid()));

			ccVo.setWmoney(ccWard.getWmoney().toString());
			ccVo.setStorey_id(ccWard.getWstoId().longValue());
			ccVo.setSto_name(ccSDAO.queryName(ccVo.getStorey_id()));

			CcStoreyVo cs = new CcStoreyVo();
			CcStorey cst = ccSDAO.findOne(ccVo.getStorey_id());
			cs.setFloor_id(cst.getSfloorId().longValue());
			ccVo.setFloor_id(cs.getFloor_id());
			ccVo.setFloor_name(ccHospDAO.queryName(ccVo.getFloor_id()));
			ccVo.setAddress(ccVo.getFloor_name() + ccVo.getSto_name());

			listVo.add(ccVo);
		}
		return listVo;
	}

}
